/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.viewlists;

import com.google.common.collect.Lists;
import com.google.inject.Singleton;
import listo.client.Convert;
import org.apache.commons.lang.ArrayUtils;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

@Singleton
public class ViewListManager implements ListSelectionListener {

    public interface ViewSelectionListener {
        public void viewSelectionChanged(View[] selectedViews);
    }

    private ViewList[] lists = new ViewList[0];
    private final List<ViewSelectionListener> listeners = Lists.newArrayList();

    private ViewList lastListSelected;

    public ViewList getLastListSelected() {
        return lastListSelected;
    }

    protected void register(ViewList list) {
        list.addListSelectionListener(this);
        lists = (ViewList[]) ArrayUtils.add(lists, list);

        // we simply keep the list sorted according the class names which fits the desired order
        Arrays.sort(lists, new Comparator<ViewList>() {
            public int compare(ViewList a, ViewList b) {
                return a.getClass().getName().compareTo(b.getClass().getName());
            }
        });
    }

    public void valueChanged(ListSelectionEvent e) {
        ViewList list = (ViewList) e.getSource();

        if (list.getSelectedValues().length > 0) {
            // if we received the message that a list has a selection that is valid
            // we need to clear the selections in all other lists
            for (ViewList viewList : lists) {
                if (viewList != list) viewList.clearSelection();
            }

            lastListSelected = list;

            // and fire the update event on the listeners
            View[] selectedViews = Convert.toViews(list.getSelectedValues());
            for (ViewSelectionListener listener : listeners) {
                listener.viewSelectionChanged(selectedViews);
            }
        }
    }

    public void addViewListSelectionListener(ViewSelectionListener listener) {
        listeners.add(listener);
    }

    public ViewList getPreviousList(ViewList list) {
        int ix = ArrayUtils.indexOf(lists, list);
        return ix < 1 ? null : lists[ix - 1];
    }

    public ViewList getNextList(ViewList list) {
        int ix = ArrayUtils.indexOf(lists, list);
        return ix == lists.length - 1 ? null : lists[ix + 1];
    }

}
